package com.example.redcultural;

import android.content.Context;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;

import java.util.HashMap;
import java.util.List;

import static com.bumptech.glide.gifdecoder.GifHeaderParser.TAG;

public class video_Adapter extends ArrayAdapter<StudyContent> {
    private LayoutInflater layoutInflater;
    private Context context;

    public video_Adapter(Context context, List<StudyContent> studyContents) {
        super(context, R.layout.video_list, R.id.video_url, studyContents);
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        View view;
        final ViewHolder viewHolder;
        StudyContent studyContent = getItem(position);

        if (convertView == null) {
            view = layoutInflater.inflate(R.layout.video_list, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.title = view.findViewById(R.id.video_title);
            viewHolder.videoView = view.findViewById(R.id.video_url);
            viewHolder.imageView=view.findViewById(R.id.video_image);
            view.setTag(viewHolder);
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.title.setText(studyContent.getTitle());
        MediaController controller = new MediaController(context);
        viewHolder.videoView.setVideoURI(Uri.parse(studyContent.getVideoUrl()));
            controller.setMediaPlayer(viewHolder.videoView);

        final MediaMetadataRetriever mmr = new MediaMetadataRetriever();
        mmr.setDataSource(studyContent.getVideoUrl(),new HashMap<String, String>());
        viewHolder.imageView.setImageBitmap(mmr.getFrameAtTime());
        viewHolder.videoView.setMediaController(controller);
        viewHolder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewHolder.imageView.setVisibility(View.GONE);
                viewHolder.videoView.start();
            }
        });
        viewHolder.videoView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(viewHolder.videoView.isPlaying())
                {
                    viewHolder.imageView.setVisibility(View.INVISIBLE);
                    return false;
                }
                    return true;
            }
        });
        return view;
    }

    class ViewHolder {
        TextView title;
        VideoView videoView;
        ImageView imageView;
    }


}
